<?php

require_once('./lib/sort.lib.php');

function position_sort($a, $b)
{
	if ($a == "g") $a = 0;
	else if ($a == "dr") $a = 1;
	else if ($a == "dc") $a = 2;
	else if ($a == "dl") $a = 3;
	else if ($a == "dm") $a = 4;
	else if ($a == "cm") $a = 5;
	else if ($a == "am") $a = 6;
	else if ($a == "fw") $a = 7;
	else $a = 8;
	if ($b == "g") $b = 0;
	else if ($b == "dr") $b = 1;
	else if ($b == "dc") $b = 2;
	else if ($b == "dl") $b = 3;
	else if ($b == "dm") $b = 4;
	else if ($b == "cm") $b = 5;
	else if ($b == "am") $b = 6;
	else if ($b == "fw") $b = 7;
	else $b = 8;

	if ($a > $b) return 1;
	else if ($a < $b) return -1;
	else return 0;
}

function write_game_score($row, $logo = true)
{
	$ret .= "<table class='game_score'><tr>\n";
        $ret .= "<td class='game_time'>";
        if (substr($row["date"], 11, 100) !== "00:00:00") {
                $ret .= date("H:i", strtotime($row["date"]));
        }
        $ret .= "</td>\n";
	if ($logo)
	{
		$ret .= "<td>";
		if ($row["home_logo"])
		{
			$home_logo = preg_replace("/\.svg/", "_15.png", $row["home_logo"]);
			$ret .= "<img src='img/logos/$home_logo' width=15 height=15>";
		}
		else
		{
			$ret .= "<img src='img/logos/not_found.png' width=15 height=15>";
		}
		$ret .= "</td>\n";
	}
	$ret .= "<td class='home_team'>".$row["home_name"]."</td>\n";
	$ret .= "<td class='home_score'>".$row["home_score"]."</td>\n";
	$ret .= "<td class='game_x'>x</td>\n";
	$ret .= "<td class='away_score'>".$row["away_score"]."</td>\n";
	$ret .= "<td class='away_team'>".$row["away_name"]."</td>\n";
	if ($logo)
	{
		$ret .= "<td>";
		if ($row["away_logo"])
		{
			$away_logo = preg_replace("/\.svg/", "_15.png", $row["away_logo"]);
			$ret .= "<img src='img/logos/$away_logo' width=15 height=15>";
		}
		else
		{
			$ret .= "<img src='img/logos/not_found.png' width=15 height=15>";
		}
		$ret .= "</td>\n";
	}
	$ret .= "</tr></table>\n";

	return $ret;
}

function write_game_info($row)
{
	$ret .= "<div class='close'><div class='X' onClick='hideInfo(this.parentNode.parentNode.parentNode.parentNode);'>x</div><div>&nbsp;</div></div>\n";

	$query = "SELECT Player.name, Goal.time, Goal.team, Goal.penalty, Goal.own_goal, Player.id FROM Goal, Player WHERE Goal.player = Player.id AND game = ".$row["game_id"]." ORDER BY Goal.time;";
	$result_goal = mysql_query($query);
	$num_goal = mysql_numrows($result_goal);
	global $count_home;
	global $count_away;
	$count_home = 0;
	$count_away = 0;
	for ($l = 0; $l < $num_goal; $l++)
	{
		$player_id = mysql_result($result_goal, $l, "Player.id");
		$player_name = mysql_result($result_goal, $l, "Player.name");
		$goal_time = mysql_result($result_goal, $l, "Goal.time");
		$goal_team = mysql_result($result_goal, $l, "Goal.team");
		$goal_pen = mysql_result($result_goal, $l, "Goal.penalty");
		$goal_own = mysql_result($result_goal, $l, "Goal.own_goal");

		if ($goal_time < 10)
			$goal_time = "0" . $goal_time;

		$ret .= "<table class='goal full'><tr>\n";
                $ret .= "<td class='game_time'></td>\n";
		if ($goal_team == $row["home"] and $goal_own == 0 or $goal_team == $row["away"] and $goal_own == 1)
		{
			$count_home++;
			$ret .= "<td class='home_goal'>$player_name</td>";
			if ($goal_pen) $ret .= "<td class='home_pen'>(Pen)</td>";
			else if ($goal_own) $ret .= "<td class='home_pen'>(Own)</td>";
			else $ret .= "<td class='home_pen'></td>";
			$ret .= "<td class='home_time'>$goal_time' ($count_home-$count_away)</td>\n";
			$ret .= "<td></td><td></td><td></td>\n";
		}
		else
		{
			$count_away++;
			$ret .= "<td></td><td></td><td></td>\n";
			$ret .= "<td class='away_time'>($count_home-$count_away) $goal_time'</td>";
			if ($goal_pen) $ret .= "<td class='away_pen'>(Pen)</td>";
			else if ($goal_own) $ret .= "<td class='away_pen'>(Own)</td>";
			else $ret .= "<td class='away_pen'></td>";
			$ret .= "<td class='away_goal'>$player_name</td>\n";
		}
		$ret .= "</tr></table>\n";
	}

	if (isset($row["home_pen"]) or isset($row["away_pen"]))
	{
		$ret .= "<table class='full pen_line'><tr>\n";
		$ret .= "<td class='home_goal'>Penalties:</td>";
		$ret .= "<td class='home_pen'></td>";
		$ret .= "<td class='home_time'>".$row["home_pen"]." x </td>\n";
		$ret .= "<td class='away_time'>".$row["away_pen"]."</td>\n";
		$ret .= "<td class='away_pen'></td>";
		$ret .= "<td class='away_goal'></td>\n";
		$ret .= "</tr></table>\n";
	}

	$ret .= "<br>\n";

	if ($row["referee_name"])
	{
		$ret .= "<table class='info_line full'><tr>\n";
		$ret .= "<td class='info_caption'>Referee:</td>\n";
		$ret .= "<td class='info'>".$row["referee_name"] . ($row["referee_location"] ? " (".$row["referee_location"].")" : "") . "</td>\n";
		$ret .= "</tr></table>\n";
	}
	if ($row["stadium_name"])
	{
		$ret .= "<table class='info_line full'><tr>\n";
		$ret .= "<td class='info_caption'>Stadium:</td>\n";
		$ret .= "<td class='info'>".$row["stadium_name"]."</td>\n";
		$ret .= "</tr></table>\n";
	}
	if ($row["attendance"])
	{
		$ret .= "<table class='info_line full'><tr>\n";
		$ret .= "<td class='info_caption'>Attendance:</td>\n";
		$ret .= "<td class='info'>".$row["attendance"]."</td>\n";
		$ret .= "</tr></table>\n";
	}

	$ret .= "<div id='game_squad_".$row["game_id"]."'><span class='squad_click' onClick='write_squad(this.parentNode, ".$row["game_id"].", null);'>Show squad</span></div>\n";

	return $ret;
}

function write_game_squad($row)
{
	$squad[$row["home"]]["play"] = array();
	$squad[$row["home"]]["bench"] = array();
	$squad[$row["away"]]["play"] = array();
	$squad[$row["away"]]["bench"] = array();

	$query = "SELECT Player.id id, name, position, `on`, off, yellow, red, team FROM Player, player_game WHERE player_game.game = ".$row["game_id"]." AND Player.id = player_game.player;";
	$result_squad = mysql_query($query);
	$num_squad = mysql_numrows($result_squad);
	for ($i = 0; $i < $num_squad; $i++)
	{
		$id = mysql_result($result_squad, $i, "id");
		$name = mysql_result($result_squad, $i, "name");
		$position = mysql_result($result_squad, $i, "position");
		$on = mysql_result($result_squad, $i, "on");
		$off = mysql_result($result_squad, $i, "off");
		$yellow = mysql_result($result_squad, $i, "yellow");
		$red = mysql_result($result_squad, $i, "red");
		$team = mysql_result($result_squad, $i, "team");

		$query = "SELECT id FROM Goal WHERE game = ".$row["game_id"]." AND player = ".$id.";";
		$result_goal = mysql_query($query);
		$goal = mysql_numrows($result_goal);

		if ($on == 0 and $off == 0)
			$squad[$team]["bench"][] = array("name" => $name, "position" => $position, "on" => $on, "off" => $off, "yellow" => $yellow, "red" => $red, "goal" => $goal);
		else
			$squad[$team]["play"][] = array("name" => $name, "position" => $position, "on" => $on, "off" => $off, "yellow" => $yellow, "red" => $red, "goal" => $goal);
	}
	
	$squad[$row["home"]]["play"] = multi_sort($squad[$row["home"]]["play"], array(array("on", true), array("position", true, "position_sort"), array("name", true)), true, true);
	$squad[$row["away"]]["play"] = multi_sort($squad[$row["away"]]["play"], array(array("on", true), array("position", true, "position_sort"), array("name", true)), true, true);
	$squad[$row["home"]]["bench"] = multi_sort($squad[$row["home"]]["bench"], array(array("on", true), array("position", true, "position_sort"), array("name", true)), true, true);
	$squad[$row["away"]]["bench"] = multi_sort($squad[$row["away"]]["bench"], array(array("on", true), array("position", true, "position_sort"), array("name", true)), true, true);

	$ret .= "<table class='full'>\n";
	$ret .= "<tr><td class='game_time'></td>";
        $ret .= "<td><a href='javascript:edit_squad(";
	$ret .= $row["game_id"];
	$ret .= ", ";
	$ret .= $row["home"];
	$ret .= ");'>Edit</a></td><td><a href='javascript:edit_squad(";
	$ret .= $row["game_id"];
	$ret .= ", ";
	$ret .= $row["away"];
	$ret .= ");'>Edit</a></td></tr>\n";
	$ret .= "<tr><td class='game_time'></td>";
        $ret .= "<td valign='top' width='47%'>\n";
	$ret .= "<table class='full'><tr><th align='left'>Player</th><th align='right'>Pos</th><th align='right'>On</th><th align='right'>Off</th><th>&nbsp;</th><th>&nbsp;</th></tr>\n";
	for ($i = 0; $i < count($squad[$row["home"]]["play"]); $i++)
	{
		$ret .= "<tr><td class='squad_player'>";
		$ret .= $squad[$row["home"]]["play"][$i]["name"];
		$ret .= "</td><td class='squad_position'>";
		$ret .= $squad[$row["home"]]["play"][$i]["position"];
		$ret .= "</td><td class='squad_on'>";
		$ret .= $squad[$row["home"]]["play"][$i]["on"];
		$ret .= "</td><td class='squad_off'>";
		$ret .= $squad[$row["home"]]["play"][$i]["off"];
		$ret .= "</td><td class='squad_yellow'>";
		if ($squad[$row["home"]]["play"][$i]["yellow"])
			$ret .= "<img src='img/yells.gif'>";
		$ret .= "</td><td class='squad_red'>";
		if ($squad[$row["home"]]["play"][$i]["red"])
			$ret .= "<img src='img/reds.gif'>";
		$ret .= "</td><td class='squad_goal'>";
		if ($squad[$row["home"]]["play"][$i]["goal"])
			$ret .= "<img src='img/goal.png'>";
		$ret .= "</td></tr>\n";
		if ($i == 10)
			$ret .= "<tr><td colspan='6'><hr></td></tr>\n";
	}

	for ($i = 0; $i < count($squad[$row["home"]]["bench"]); $i++)
	{
		$ret .= "<tr><td class='squad_player'>";
		$ret .= $squad[$row["home"]]["bench"][$i]["name"];
		$ret .= "</td><td class='squad_position'>";
		$ret .= $squad[$row["home"]]["bench"][$i]["position"];
		$ret .= "</td><td class='squad_on'>";
		$ret .= $squad[$row["home"]]["bench"][$i]["on"];
		$ret .= "</td><td class='squad_off'>";
		$ret .= $squad[$row["home"]]["bench"][$i]["off"];
		$ret .= "</td><td class='squad_yellow'>";
		if ($squad[$row["home"]]["bench"][$i]["yellow"])
			$ret .= "<img src='img/yells.gif'>";
		$ret .= "</td><td class='squad_red'>";
		if ($squad[$row["home"]]["bench"][$i]["red"])
			$ret .= "<img src='img/reds.gif'>";
		$ret .= "</td><td class='squad_goal'>";
		if ($squad[$row["home"]]["bench"][$i]["goal"])
			$ret .= "<img src='img/goal.png'>";
		$ret .= "</td></tr>";
	}

	$ret .= "</table></td><td valign='top' width='47%'>\n";
	$ret .= "<table class='full'><tr><th align='left'>Player</th><th align='right'>Pos</th><th align='right'>On</th><th align='right'>Off</th><th>&nbsp;</th><th>&nbsp;</th></tr>\n";
	
	for ($i = 0; $i < count($squad[$row["away"]]["play"]); $i++)
	{
		$ret .= "<tr><td class='squad_player'>";
		$ret .= $squad[$row["away"]]["play"][$i]["name"];
		$ret .= "</td><td class='squad_position'>";
		$ret .= $squad[$row["away"]]["play"][$i]["position"];
		$ret .= "</td><td class='squad_on'>";
		$ret .= $squad[$row["away"]]["play"][$i]["on"];
		$ret .= "</td><td class='squad_off'>";
		$ret .= $squad[$row["away"]]["play"][$i]["off"];
		$ret .= "</td><td class='squad_yellow'>\n";
		if ($squad[$row["away"]]["play"][$i]["yellow"])
			$ret .= "<img src='img/yells.gif'>";
		$ret .= "</td><td class='squad_red'>";
		if ($squad[$row["away"]]["play"][$i]["red"])
			$ret .= "<img src='img/reds.gif'>";
		$ret .= "</td><td class='squad_goal'>";
		if ($squad[$row["away"]]["play"][$i]["goal"])
			$ret .= "<img src='img/goal.png'>";
		$ret .= "</td></tr>";
		if ($i == 10)
			$ret .= "<tr><td colspan='6'><hr></td></tr>\n";
	}

	for ($i = 0; $i < count($squad[$row["away"]]["bench"]); $i++)
	{
		$ret .= "<tr><td class='squad_player'>";
		$ret .= $squad[$row["away"]]["bench"][$i]["name"];
		$ret .= "</td><td class='squad_position'>";
		$ret .= $squad[$row["away"]]["bench"][$i]["position"];
		$ret .= "</td><td class='squad_on'>";
		$ret .= $squad[$row["away"]]["bench"][$i]["on"];
		$ret .= "</td><td class='squad_off'>";
		$ret .= $squad[$row["away"]]["bench"][$i]["off"];
		$ret .= "</td><td class='squad_yellow'>\n";
		if ($squad[$row["away"]]["bench"][$i]["yellow"])
			$ret .= "<img src='img/yells.gif'>";
		$ret .= "</td><td class='squad_red'>";
		if ($squad[$row["away"]]["bench"][$i]["red"])
			$ret .= "<img src='img/reds.gif'>";
		$ret .= "</td><td class='squad_goal'>";
		if ($squad[$row["away"]]["bench"][$i]["goal"])
			$ret .= "<img src='img/goal.png'>";
		$ret .= "</td></tr>";
	}

	$ret .= "</table></td></tr>\n";
	$ret .= "</table>\n";

	return $ret;
}
?>
